/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package project.jpdftools.animation;

import java.awt.Color;
import java.awt.Font;
import javax.swing.*;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.TitledBorder;
import project.jpdftools.other.RandomColor;

/**
 *
 * @author Mido
 */
public class AnimationWorker extends SwingWorker<Void, Void> {

    private boolean killSignal = false;
    private String actionMessage;
    private JDialog dialog = new JDialog();
    private JTextField tfText = new JTextField();
    private JPanel pLoading = new JPanel();
    private JButton[] buttons;

    public AnimationWorker(String action) {
        this.actionMessage = action;
        initComponents();

        // center the frame on the screen
        project.jpdftools.toolkit.Toolkit.centerWindow(dialog, dialog.getHeight(), dialog.getWidth());
    }

    /**
     * Main task. Executed in background thread.
     *
     * @return
     */
    @Override
    protected Void doInBackground() throws InterruptedException {
        // show the frame
        dialog.setVisible(true);
        Color firstColor = RandomColor.getRandomColor();
        Color secondColor = RandomColor.getRandomColor();
        Color thirdColor = RandomColor.getRandomColor();

        while (killSignal == false) {
            tfText.setText(actionMessage + " .");
            for (int i = 0; i < buttons.length; i++) {
                if (i == 12) {
                    buttons[i].setBackground(firstColor);
                } else {
                    buttons[i].setBackground(Color.white);
                }
            }
            Thread.sleep(300);
            tfText.setText(actionMessage + " ..");
            for (int i = 0; i < buttons.length; i++) {
                if (i == 12) {
                    buttons[i].setBackground(firstColor);
                } else if ((i >= 6 && i <= 8) || (i >= 16 && i <= 18) || i == 11 || i == 13) {
                    buttons[i].setBackground(secondColor);
                } else {
                    buttons[i].setBackground(Color.white);
                }
            }
            Thread.sleep(300);
            tfText.setText(actionMessage + " ...");
            for (int i = 0; i < buttons.length; i++) {
                if (i == 12) {
                    buttons[i].setBackground(firstColor);
                } else if ((i >= 6 && i <= 8) || (i >= 16 && i <= 18) || i == 11 || i == 13) {
                    buttons[i].setBackground(secondColor);
                } else if ((i >= 0 && i <= 4) || (i >= 20 && i <= 24) || i == 5 || i == 10 || i == 15 || i == 9 || i == 14 || i == 19) {
                    buttons[i].setBackground(thirdColor);
                } else {
                    buttons[i].setBackground(Color.white);
                }
            }
            Thread.sleep(300);
        }
        return null;
    }

    /**
     * Executed in event dispatching thread
     */
    @Override
    public void done() {
        // dispose the frame
        dialog.dispose();
    }

    private void initComponents() {
        JButton bt_1 = new JButton();
        JButton bt_2 = new JButton();
        JButton bt_3 = new JButton();
        JButton bt_4 = new JButton();
        JButton bt_5 = new JButton();
        JButton bt_6 = new JButton();
        JButton bt_7 = new JButton();
        JButton bt_8 = new JButton();
        JButton bt_9 = new JButton();
        JButton bt_10 = new JButton();
        JButton bt_11 = new JButton();
        JButton bt_12 = new JButton();
        JButton bt_13 = new JButton();
        JButton bt_14 = new JButton();
        JButton bt_15 = new JButton();
        JButton bt_16 = new JButton();
        JButton bt_17 = new JButton();
        JButton bt_18 = new JButton();
        JButton bt_19 = new JButton();
        JButton bt_20 = new JButton();
        JButton bt_21 = new JButton();
        JButton bt_22 = new JButton();
        JButton bt_23 = new JButton();
        JButton bt_24 = new JButton();
        JButton bt_25 = new JButton();


        buttons = new JButton[]{bt_1, bt_2, bt_3, bt_4, bt_5, bt_6, bt_7, bt_8, bt_9, bt_10, bt_11, bt_12, bt_13, bt_14, bt_15, bt_16, bt_17, bt_18,
            bt_19, bt_20, bt_21, bt_22, bt_23, bt_24, bt_25};

        // loop on all the buttons and apply the settings
        for (int i = 0; i < buttons.length; i++) {
            buttons[i].setBorder(null);
            buttons[i].setDefaultCapable(false);
            buttons[i].setFocusPainted(false);
            buttons[i].setFocusable(false);
            buttons[i].setRolloverEnabled(false);
        }

        dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        dialog.setAlwaysOnTop(true);
        dialog.setResizable(false);
        dialog.setUndecorated(true);

        pLoading.setBorder(BorderFactory.createTitledBorder(null, actionMessage, TitledBorder.LEFT, TitledBorder.TOP));

        tfText.setEditable(false);
        tfText.setFont(new Font("Times New Roman", 0, 18));
        tfText.setHorizontalAlignment(JTextField.CENTER);
        tfText.setBorder(null);
        tfText.setOpaque(false);

        GroupLayout pLoadingLayout = new GroupLayout(pLoading);
        final int preferredGroupSize = GroupLayout.PREFERRED_SIZE;
        pLoading.setLayout(pLoadingLayout);
        pLoadingLayout.setHorizontalGroup(pLoadingLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(pLoadingLayout.createSequentialGroup().addContainerGap().addGroup(pLoadingLayout.createParallelGroup(GroupLayout.Alignment.CENTER).addGroup(pLoadingLayout.createSequentialGroup().addComponent(bt_21, preferredGroupSize, 41, preferredGroupSize).addPreferredGap(ComponentPlacement.RELATED).addComponent(bt_22, preferredGroupSize, 41, preferredGroupSize).addPreferredGap(ComponentPlacement.RELATED).addComponent(bt_23, preferredGroupSize, 41, preferredGroupSize).addPreferredGap(ComponentPlacement.RELATED).addComponent(bt_24, preferredGroupSize, 41, preferredGroupSize).addPreferredGap(ComponentPlacement.RELATED).addComponent(bt_25, preferredGroupSize, 41, preferredGroupSize)).addGroup(pLoadingLayout.createSequentialGroup().addComponent(bt_16, preferredGroupSize, 41, preferredGroupSize).addPreferredGap(ComponentPlacement.RELATED).addComponent(bt_17, preferredGroupSize, 41, preferredGroupSize).addPreferredGap(ComponentPlacement.RELATED).addComponent(bt_18, preferredGroupSize, 41, preferredGroupSize).addPreferredGap(ComponentPlacement.RELATED).addComponent(bt_19, preferredGroupSize, 41, preferredGroupSize).addPreferredGap(ComponentPlacement.RELATED).addComponent(bt_20, preferredGroupSize, 41, preferredGroupSize)).addGroup(pLoadingLayout.createSequentialGroup().addComponent(bt_11, preferredGroupSize, 41, preferredGroupSize).addPreferredGap(ComponentPlacement.RELATED).addComponent(bt_12, preferredGroupSize, 41, preferredGroupSize).addPreferredGap(ComponentPlacement.RELATED).addComponent(bt_13, preferredGroupSize, 41, preferredGroupSize).addPreferredGap(ComponentPlacement.RELATED).addComponent(bt_14, preferredGroupSize, 41, preferredGroupSize).addPreferredGap(ComponentPlacement.RELATED).addComponent(bt_15, preferredGroupSize, 41, preferredGroupSize)).addGroup(pLoadingLayout.createSequentialGroup().addComponent(bt_6, preferredGroupSize, 41, preferredGroupSize).addPreferredGap(ComponentPlacement.RELATED).addComponent(bt_7, preferredGroupSize, 41, preferredGroupSize).addPreferredGap(ComponentPlacement.RELATED).addComponent(bt_8, preferredGroupSize, 41, preferredGroupSize).addPreferredGap(ComponentPlacement.RELATED).addComponent(bt_9, preferredGroupSize, 41, preferredGroupSize).addPreferredGap(ComponentPlacement.RELATED).addComponent(bt_10, preferredGroupSize, 41, preferredGroupSize)).addGroup(pLoadingLayout.createSequentialGroup().addComponent(bt_1, preferredGroupSize, 41, preferredGroupSize).addPreferredGap(ComponentPlacement.RELATED).addComponent(bt_2, preferredGroupSize, 41, preferredGroupSize).addPreferredGap(ComponentPlacement.RELATED).addComponent(bt_3, preferredGroupSize, 41, preferredGroupSize).addPreferredGap(ComponentPlacement.RELATED).addComponent(bt_4, preferredGroupSize, 41, preferredGroupSize).addPreferredGap(ComponentPlacement.RELATED).addComponent(bt_5, preferredGroupSize, 41, preferredGroupSize)).addComponent(tfText, GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)).addContainerGap()));
        pLoadingLayout.setVerticalGroup(pLoadingLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(pLoadingLayout.createSequentialGroup().addGroup(pLoadingLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(bt_2, preferredGroupSize, 27, preferredGroupSize).addComponent(bt_1, preferredGroupSize, 27, preferredGroupSize).addComponent(bt_3, preferredGroupSize, 27, preferredGroupSize).addComponent(bt_4, preferredGroupSize, 27, preferredGroupSize).addComponent(bt_5, preferredGroupSize, 27, preferredGroupSize)).addPreferredGap(ComponentPlacement.RELATED).addGroup(pLoadingLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(bt_7, preferredGroupSize, 27, preferredGroupSize).addComponent(bt_6, preferredGroupSize, 27, preferredGroupSize).addComponent(bt_8, preferredGroupSize, 27, preferredGroupSize).addComponent(bt_9, preferredGroupSize, 27, preferredGroupSize).addComponent(bt_10, preferredGroupSize, 27, preferredGroupSize)).addPreferredGap(ComponentPlacement.RELATED).addGroup(pLoadingLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(bt_12, preferredGroupSize, 27, preferredGroupSize).addComponent(bt_11, preferredGroupSize, 27, preferredGroupSize).addComponent(bt_13, preferredGroupSize, 27, preferredGroupSize).addComponent(bt_14, preferredGroupSize, 27, preferredGroupSize).addComponent(bt_15, preferredGroupSize, 27, preferredGroupSize)).addPreferredGap(ComponentPlacement.RELATED).addGroup(pLoadingLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(bt_17, preferredGroupSize, 27, preferredGroupSize).addComponent(bt_16, preferredGroupSize, 27, preferredGroupSize).addComponent(bt_18, preferredGroupSize, 27, preferredGroupSize).addComponent(bt_19, preferredGroupSize, 27, preferredGroupSize).addComponent(bt_20, preferredGroupSize, 27, preferredGroupSize)).addGap(3, 3, 3).addGroup(pLoadingLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(bt_22, preferredGroupSize, 27, preferredGroupSize).addComponent(bt_21, preferredGroupSize, 27, preferredGroupSize).addComponent(bt_23, preferredGroupSize, 27, preferredGroupSize).addComponent(bt_24, preferredGroupSize, 27, preferredGroupSize).addComponent(bt_25, preferredGroupSize, 27, preferredGroupSize)).addPreferredGap(ComponentPlacement.RELATED).addComponent(tfText, GroupLayout.DEFAULT_SIZE, 37, Short.MAX_VALUE).addContainerGap()));

        GroupLayout layout = new GroupLayout(dialog.getContentPane());
        dialog.getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addContainerGap().addComponent(pLoading, preferredGroupSize, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addContainerGap()));
        layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addContainerGap().addComponent(pLoading, preferredGroupSize, GroupLayout.DEFAULT_SIZE, preferredGroupSize).addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

        dialog.pack();
    }

    public void setKillSignal(boolean killSignal) {
        this.killSignal = killSignal;
    }
}
